<?php
	include_once("global.php");
	session_start();
	$uid = $_SESSION['name'];
	$smarty->assign("uid",$uid);
	//头部
	$sql_select_search = "select K_Name,K_KID from keywordinfo where K_search > '1' limit 0,4";
	$query_select_search = $mysqli->query($sql_select_search);
	while($result_select_search = $query_select_search->fetch_assoc()){
		$assign_search[] = array(	"num"	=>	$result_select_search['K_KID'],
									"name"	=>	$result_select_search['K_Name']);
	}
	$smarty->assign("assign_search",$assign_search);/*热门关键词*/
	
	$sql_select_skeyword = "select K_Name,K_KID from keywordinfo where K_search = '1'";
	$query_select_skeyword = $mysqli->query($sql_select_skeyword);
	while($result_select_skeyword = $query_select_skeyword->fetch_assoc()){
		$assign_skeyword[] = array(	"num"	=>	$result_select_skeyword['K_KID'],
									"name"	=>	$result_select_skeyword['K_Name']);
	}
	$smarty->assign("assign_skeyword",$assign_skeyword);/*搜索框内关键词*/
	
	$sql_select_articlecategory = "select * from categoryinfo limit 0,2";
	$query_select_articlecategory = $mysqli->query($sql_select_articlecategory);
	while($result_select_articlecategory = $query_select_articlecategory->fetch_assoc()){
		$assign_artcategory[] = array (	"id"	=>	$result_select_articlecategory['C_CID'],
										"name"	=>	$result_select_articlecategory['C_Categoryname']);
	}
	$smarty->assign("assign_artcategory",$assign_artcategory);/*导航条*/
	
	$sql_select_countapp = "select * from appinfo";
	$query_select_countapp = $mysqli->query($sql_select_countapp);
	$result_select_countapp = $query_select_countapp->num_rows;
	$assign_countapp = $result_select_countapp;
	$smarty->assign("assign_countapp",$assign_countapp);/*站内APP统计*/
	
	$random = mt_rand(50,$assign_countapp);
	$assign_radnom = $random;
	$smarty->assign("assign_radnom",$assign_radnom);	
	
	//通栏推广位
	$sql_select_banner = "select * from pushinfo where P_Pushid >30";
	$query_select_banner = $mysqli->query($sql_select_banner);
	while($result_select_banner = $query_select_banner->fetch_assoc()){
		$banner_appid = $result_select_banner['P_Pushappid'];
		$banner_apppid = $result_select_banner['P_Pushpid'];
		
		$sql_select_bannerpic = "select * from pictureinfo where P_PID = '$banner_apppid'";
		$query_select_bannerpic = $mysqli->query($sql_select_bannerpic);
		$result_select_bannerpic = $query_select_bannerpic->fetch_assoc();
		
		$sql_select_bannerapp = "select * from appinfo where A_Apptrackid = '$banner_appid'";
		$query_select_bannerapp = $mysqli->query($sql_select_bannerapp);
		$result_select_bannerapp = $query_select_bannerapp->fetch_assoc();
		
		$assign_bannerapp[] = array(	"id"	=>	$banner_appid,
										"name"	=>	$result_select_bannerapp['A_Apptrackname'],
										"url"	=>	"images/".$result_select_bannerpic['P_Name'].".png",);
	}
	$smarty->assign("assign_bannerapp",$assign_bannerapp);/*顶部通栏，首页通栏1，首页通栏2*/
	
	//右侧
	//右侧文章推广
	$sql_select_right = "select P_Pushpid,P_Pushartid from pushinfo where P_Pushid >'18' and P_Pushid < '21'";
	$query_select_right = $mysqli->query($sql_select_right);
	while($result_select_right = $query_select_right->fetch_assoc()){
		$pid = $result_select_right['P_Pushpid'];
		$artid = $result_select_right['P_Pushartid'];

		$sql_select_rightpid = "select P_Name from pictureinfo where P_PID = '$pid'";
		$query_select_rightpid = $mysqli->query($sql_select_rightpid);
		$result_select_rightpid = $query_select_rightpid->fetch_assoc();
		$pic = $result_select_rightpid['P_Name'];
		
		$sql_select_rightari = "select A_Title from articleinfo where A_AID = '$artid'";
		$query_select_rightari = $mysqli->query($sql_select_rightari);
		$result_select_rightari = $query_select_rightari->fetch_assoc();
		
		$assign_right[] = array(	"id"	=>	$artid,
									"pic"	=>	$pic,
									"name"	=>	$result_select_rightari['A_Title']);
	}
	$smarty->assign("assign_right",$assign_right);/*右侧文章推广*/
	
	$sql_select_rightapp = "select P_Pushappid,P_Pushpid from pushinfo where P_Pushid >'20' and P_Pushid < '23'";
	$query_select_rightapp = $mysqli->query($sql_select_rightapp);
	while($result_select_rightapp = $query_select_rightapp->fetch_assoc()){
		$app_pid = $result_select_rightapp['P_Pushpid'];
		$app_id = $result_select_rightapp['P_Pushappid'];

		$sql_select_rightapppid = "select P_Name from pictureinfo where P_PID = '$app_pid'";
		$query_select_rightapppid = $mysqli->query($sql_select_rightapppid);
		$result_select_rightapppid = $query_select_rightapppid->fetch_assoc();
		$apppic = $result_select_rightapppid['P_Name'];
		
		$sql_select_rightappname = "select A_Apptrackname from appinfo where A_Apptrackid = '$app_id'";
		$query_select_rightappname = $mysqli->query($sql_select_rightappname);
		$result_select_rightappname = $query_select_rightappname->fetch_assoc();
		
		$assign_rightapp[] = array(	"id"	=>	$app_id,
									"pic"	=>	$apppic,
									"name"	=>	$result_select_rightappname['A_Apptrackname']);
	}
	$smarty->assign("assign_rightapp",$assign_rightapp);/*右侧APP推广*/
	//文章列表
	$sql_select_newstop5 = "select A_AID,A_Title,A_CID from articleinfo where A_CID='1' order by A_AID desc limit 0,5";
	$query_select_newstop5 = $mysqli->query($sql_select_newstop5);
	while($result_select_newstop5 = $query_select_newstop5->fetch_assoc()){
		$assign_newstop5[] = array(	"id"		=>	$result_select_newstop5['A_AID'],
								"category"	=>	$result_select_newstop5['A_CID'],
								"title"		=>	$result_select_newstop5['A_Title']
								);
	}
	$smarty->assign("assign_newstop5",$assign_newstop5);/*新闻前五篇*/
	$sql_select_newstop10 = "select A_AID,A_Title,A_CID from articleinfo where A_CID='1' order by A_AID desc limit 5,5";
	$query_select_newstop10 = $mysqli->query($sql_select_newstop10);
	while($result_select_newstop10 = $query_select_newstop10->fetch_assoc()){
		$assign_newstop10[] = array(	"id"		=>	$result_select_newstop10['A_AID'],
								"category"	=>	$result_select_newstop10['A_CID'],
								"title"		=>	$result_select_newstop10['A_Title']
								);
	}
	$smarty->assign("assign_newstop10",$assign_newstop10);/*新闻后五篇*/
	
	$sql_select_newtop5 = "select A_AID,A_Title,A_CID from articleinfo where A_CID='2' order by A_AID desc limit 0,5";
	$query_select_newstop5 = $mysqli->query($sql_select_newtop5);
	while($result_select_newtop5 = $query_select_newstop5->fetch_assoc()){
		$assign_newtop5[] = array(	"id"		=>	$result_select_newtop5['A_AID'],
								"category"	=>	$result_select_newtop5['A_CID'],
								"title"		=>	$result_select_newtop5['A_Title']
								);
	}
	$smarty->assign("assign_newtop5",$assign_newtop5);/*测评前五篇*/
	$sql_select_newtop10 = "select A_AID,A_Title,A_CID from articleinfo where A_CID='2' order by A_AID desc limit 5,5";
	$query_select_newtop10 = $mysqli->query($sql_select_newtop10);
	while($result_select_newtop10 = $query_select_newtop10->fetch_assoc()){
		$assign_newtop10[] = array(	"id"		=>	$result_select_newtop10['A_AID'],
								"category"	=>	$result_select_newtop10['A_CID'],
								"title"		=>	$result_select_newtop10['A_Title']
								);
	}
	$smarty->assign("assign_newtop10",$assign_newtop10);/*测评后五篇*/	
	//APP排行榜
	$sql_app_rank = "select * from appinfo where A_Appprice = '免费' order by A_Appdate desc limit 0,10";
	$query_app_rank = $mysqli->query($sql_app_rank);
	while($result_app_rank = $query_app_rank->fetch_assoc()){
		$assign_apprank1[] = array(	"id"	=>	$result_app_rank['A_Appid'],
									"name"	=>	$result_app_rank['A_Apptrackname'],
									"icon"	=>	$result_app_rank['A_Appicon'],
								);
	}
	for($m=0;$m<10;$m++){
		$assign_apprank[] = array(	"num"	=>	$m+1,
									"id"	=>	$assign_apprank1[$m][id],
									"name"	=>	$assign_apprank1[$m][name],
									"icon"	=>	$assign_apprank1[$m][icon],
								);
	}
	$smarty->assign("assign_apprank",$assign_apprank);/*免费排名*/
	
	$sql_free_rank = "select * from appinfo where A_Appprice != '免费' order by A_Applike desc limit 0,10";
	$query_free_rank = $mysqli->query($sql_free_rank);
	while($result_free_rank = $query_free_rank->fetch_assoc()){
		$assign_free1[] = array(	"id"	=>	$result_free_rank['A_Appid'],
									"name"	=>	$result_free_rank['A_Apptrackname'],
									"icon"	=>	$result_free_rank['A_Appicon'],
								);
	}
	for($i =0; $i<10;$i++){
		$assign_free[] = array(	"num"	=>	$i+1,
								"id"	=>	$assign_free1[$i][id],
								"name"	=>	$assign_free1[$i][name],
								"icon"	=>	$assign_free1[$i][icon]
								);
	}
	$smarty->assign("assign_free",$assign_free);

	
	
	//底部
	$sql_select_bottom = "select * from siteinfo";
	$query_select_bottom = $mysqli->query($sql_select_bottom);
	$count_info = $query_select_bottom->num_rows;
	while($result_select_bottom = $query_select_bottom->fetch_assoc()){
		$assign_site[] = array( "id"	=>	$result_select_bottom['S_SID'],
								"info"	=>	$result_select_bottom['S_Name']);
	}
	$bottom = $assign_site[4][info];
	
	//左侧
	$url = $_SERVER['REQUEST_URI'];
	$url_arr = parse_url($url);
	parse_str($url_arr['query'],$url_arr);
	$id = $url_arr[id];
	$page = $url_arr[page];
	
	$other_reading = $cid;
	$sql_select_other = "select * from articleinfo where A_AID != '$id' order by A_date desc limit 0,10";
	$query_select_other = $mysqli->query($sql_select_other);
	$other_count = $query_select_other->num_rows;
	while($result_select_other = $query_select_other->fetch_assoc()){
		$assign_other1[] = array(	"aid"	=>	$result_select_other['A_AID'],
									"title"	=>	$result_select_other['A_Title']);
	}
	for($z=0;$z<$other_count;$z++){
		$assign_other[] = array(	"id"	=>	$z,
									"aid"	=>	$assign_other1[$z][aid],
									"title"	=>	$assign_other1[$z][title]);
	}
	$smarty->assign("assign_other",$assign_other);/*其他阅读*/
	
	
		
	$sql_select_listpush = "select * from pushinfo where P_Pushid >22 and P_Pushid <31";
	$query_select_listpush = $mysqli->query($sql_select_listpush);
	while($result_select_listpush = $query_select_listpush->fetch_assoc()){
		$pushappid = $result_select_listpush['P_Pushappid'];
		$pushapppid = $result_select_listpush['P_Pushpid'];
		$sql_select_listpushapp = "select A_Apptrackname from appinfo where A_Apptrackid = '$pushappid'";
		$query_select_listpushapp = $mysqli->query($sql_select_listpushapp);
		$result_select_listpushapp = $query_select_listpushapp->fetch_assoc();
		$listpushname = $result_select_listpushapp['A_Apptrackname'];
	
		$sql_select_listpushpic = "select * from pictureinfo where P_PID = '$pushapppid'";
		$query_select_listpushpic = $mysqli->query($sql_select_listpushpic);
		$result_select_listpushpic = $query_select_listpushpic->fetch_assoc();
		$name = $result_select_listpushpic['P_Name'];
	
		$assign_listpush[] = array(	"id"	=>	$pushappid,
									"name"	=>	$listpushname,
									"path"	=>	"/images/".$name.".png",
									);
	}
	$smarty->assign("assign_listpush",$assign_listpush);/*列表推送位*/
	
	$sql_select_article = "select * from articleinfo where A_AID = '$id'";
	$query_select_article = $mysqli->query($sql_select_article);
	$result_select_article = $query_select_article->fetch_assoc();
	if($result_select_article){
		$kid = $result_select_article['A_KID'];
		$kid_explode = explode(",",$kid);
		$kid_count = count($kid_explode);
		for($i=0;$i<$kid_count;$i++){
			$value = $kid_explode[$i];
			$sql_select_kname = "select K_Name from keywordinfo where K_KID = '$value'";
			$query_select_kname = $mysqli->query($sql_select_kname);
			$result_select_kname = $query_select_kname->fetch_assoc();
			$kname[$i] = $result_select_kname['K_Name'];
		}
		for($j=0;$j<$kid_count;$j++){
			$name .= $kname[$j].",";
			$assign_kname = array(	"info"	=>	$name);
		}
		for($k =0;$k<$kid_count;$k++){
			$tag = $kid_explode[$k];
			$sql_select_tag = "select K_Name from keywordinfo where K_tags != '0' and K_KID = '$tag'";
			$query_select_tag = $mysqli->query($sql_select_tag);
			$result_select_tag = $query_select_tag->fetch_assoc();
			if($result_select_tag){
				$tagname[$k] = $result_select_tag['K_Name'];
				$assign_tagname[] = array(	"id"	=>	$tag,
											"info"	=>	$tagname[$k]);
			}
		
		}
		$smarty->assign("assign_tagname",$assign_tagname);/*标签*/
		$Count = $result_select_article['A_Count']+1;
		$sql_update_count = "update articleinfo set A_Count = '$Count' where A_AID = '$id'";
		$query_update_count = $mysqli->query($sql_update_count);
		
		$related_reading = $kid_explode[0];
		$sql_select_related = "select A_AID,A_Title from articleinfo where A_KID like '%$related_reading%' and A_AID != '$id' limit 0,3";
		$query_select_related = $mysqli->query($sql_select_related);
		while($result_select_related = $query_select_related->fetch_assoc()){
			$assign_related[] = array(	"id"	=>	$result_select_related['A_AID'],
										"title"	=>	$result_select_related['A_Title']);
		}
		$smarty->assign("assign_related",$assign_related);/*相关阅读*/
		
		$cid = $result_select_article['A_CID'];
		$sql_select_cname = "select C_Categoryname from categoryinfo where C_CID = '$cid'";
		$query_select_cname = $mysqli->query($sql_select_cname);
		$result_select_cname = $query_select_cname->fetch_assoc();
		$assign_id = array( "info"	=>	$id);
		$assign_title = array( "info"	=>	$result_select_article['A_Title']);
		$assign_desc = array( "info"	=>	$result_select_article['A_Desc']);
		$assign_date = array( "info"	=>	$result_select_article['A_date']);
		$assign_category = array( "info" 	=>	$result_select_cname['C_Categoryname']);
		$assign_categoryid = array( "info" 	=>	$result_select_article['A_CID']);
		$assign_count = array( "info"	=>	$result_select_article['A_Count']);
		$assign_bottom = array( "info"	=>	$bottom);
		$assign_info = array($assign_title,$assign_kname,$assign_desc,$assign_date,$assign_bottom,$assign_category,$assign_count,$assign_categoryid,$assign_id);
		$smarty->assign("assign_info",$assign_info);/*title,keyword,description,bottom,article_date,article_count*/
		
		$content = $result_select_article['A_Content'];
		$preg_content1 = "/<p>(.*)<div style=\"page-break-after\: always;\"><span>(.*)<\/span><\/div>/Usi";
		preg_match_all($preg_content1,$content,$content_arr1);
		if($content_arr1[0][0] != ''){
			$preg_content2 = "/<\/span><\/div><br \/>(.*)<br \/>/Usi";				
			preg_match_all($preg_content2,$content,$content_arr2);
			$count_content = count($content_arr1[0])+1;
			if($page == $count_content || $page < $count_content){
				for($l = 0; $l < $count_content; $l++){
					$z = $l+1;
					$pageFirst = "<a href='/article/view-".$id."-1'>首页</a>";
					$smarty->assign("pageFirst",$pageFirst);
					$pageLast = "<a href='/article/view-".$id."-".$count_content."'>尾页</a>";
					$smarty->assign("pageLast",$pageLast);
					if($z == 1 && $page == $z){
						$assign_content = $content_arr1[1][$l];
						$pagenext = $z+1;
						$pageCurrent = "<a style='width:20px; background:#006599; color:#fff;' href='/article/view-".$id."-".$z."'>".$z."</a>";
						$smarty->assign("pageCurrent",$pageCurrent);
						$pageNext = "<a style='width:20px;' href='/article/view-".$id."-".$pagenext."'>".$pagenext."</a>";
						$smarty->assign("pageNext",$pageNext);
					}
					else if($page == $z && $z != 1){
						$assign_content = $content_arr1[1][$l];
						$pagenext = $z+1;
						$pagepre = $z-1;
						$pageCurrent = "<a style='width:20px; background:#006599; color:#fff;' href='/article/view-".$id."-".$z."'>".$z."</a>";
						$smarty->assign("pageCurrent",$pageCurrent);
						$pagePre = "<a style='width:20px;' href='/article/view-".$id."-".$pagepre."'>".$pagepre."</a>";
						$smarty->assign("pagePre",$pagePre);
						$pageNext = "<a style='width:20px;' href='/article/view-".$id."-".$pagenext."'>".$pagenext."</a>";
						$smarty->assign("pageNext",$pageNext);
					}else if($page == $count_content){
						$assign_content = $content_arr2[1][0];
						$pageCurrent = "<a style='width:20px; background:#006599; color:#fff;' href='/article/view-".$id."-".$count_content."'>".$count_content."</a>";
						$smarty->assign("pageCurrent",$pageCurrent);
						$pagepre = $page-1;
						$pagePre = "<a style='width:20px;' href='/article/view-".$id."-".$pagepre."'>".$pagepre."</a>";
						$smarty->assign("pagePre",$pagePre);
						break;
					}
				}
				$smarty->assign("assign_content",$assign_content);
				$smarty->display("view.html");
				$smarty->display("index_right.html");
				$smarty->display("footer.html");
			}/*分页正确*/
			else{
				$smarty->display("header.html");
				$smarty->display("404.html");
				$smarty->display("footer.html");
			}/*分页错误，404*/
		}/*文章有分页，即文章编辑时加入了分页标签*/else{
			if($page == 1){
				$assign_content = $content;
				$smarty->assign("assign_content",$assign_content);
				$smarty->display("view.html");
				$smarty->display("view_right.html");
				$smarty->display("footer.html");
			}/*未分页文章，分页正确*/
			else{
				$smarty->display("header.html");
				$smarty->display("404.html");
				$smarty->display("footer.html");
			}/*未分页文章，分页错误，404*/
		}/*文章无分页，即文章编辑中未加入分页标签*/
	}/*文章内容存在，即url中的id存在*/
	else{
		$smarty->display("header.html");
		$smarty->display("404.html");
		$smarty->display("footer.html");
	}/*文章URL不存在ID，404*/
?>